package com.ffcs.cn.action.base.tree;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.scrollPanel.SmNoBorderScrollPanel;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
  * JTree 树示例代码
 */
public class DialogJTree extends SmDialog {
	private JTree tree;
	private JPopupMenu popupMenu;
	private JMenuItem expandMenuItem;
	private JMenuItem addMenuItem;
	private JMenuItem deleteMenuItem;
	private JMenuItem editMenuItem;
	private JMenuItem collapseMenuItem;


	public DialogJTree() {
		//设置标题
		setTitle("JTree 树示例");
		//设置初始化窗口大小
		Dimension dimension = new Dimension(300, 400);
		setSize(dimension);
		//设置最小窗口
		setMinimumSize(dimension);
		//初始化使用控件
		initComponents();
		//初始化布局，并添加控件
		initLayout();
		//初始化监听事件
		initListener();
	}

	private void initComponents(){
		//创建节点
		DefaultMutableTreeNode workspaceNode = new DefaultMutableTreeNode("工作空间(右键菜单对树操作)");

		DefaultMutableTreeNode datasourceNode = new DefaultMutableTreeNode("数据源");
		//添加子节点
		datasourceNode.add(new DefaultMutableTreeNode("DataSource1"));
		datasourceNode.add(new DefaultMutableTreeNode("DataSource2"));
		datasourceNode.add(new DefaultMutableTreeNode("DataSource3"));

		DefaultMutableTreeNode mapNode = new DefaultMutableTreeNode("地图");
		mapNode.add(new DefaultMutableTreeNode("Map1"));
		mapNode.add(new DefaultMutableTreeNode("Map2"));
		mapNode.add(new DefaultMutableTreeNode("Map3"));

		DefaultMutableTreeNode layoutNode = new DefaultMutableTreeNode("布局");
		layoutNode.add(new DefaultMutableTreeNode("Layout1"));
		layoutNode.add(new DefaultMutableTreeNode("Layout2"));
		layoutNode.add(new DefaultMutableTreeNode("Layout3"));

		workspaceNode.add(datasourceNode);
		workspaceNode.add(mapNode);
		workspaceNode.add(layoutNode);

		//创建树
		tree = new JTree(workspaceNode);
		//给根节点显示折叠按钮
		tree.setShowsRootHandles(true);

		//创建弹出菜单
		popupMenu = new JPopupMenu();
		//创建菜单项
		expandMenuItem = new JMenuItem("展开");
		collapseMenuItem = new JMenuItem("折叠");
		addMenuItem = new JMenuItem("添加");
		deleteMenuItem = new JMenuItem("删除");
		editMenuItem = new JMenuItem("编辑");

		//添加菜单项
		popupMenu.add(expandMenuItem);
		popupMenu.add(collapseMenuItem);
		popupMenu.addSeparator();
		popupMenu.add(addMenuItem);
		popupMenu.add(deleteMenuItem);
		popupMenu.add(editMenuItem);
	}

	private void initLayout(){
		//设置布局为 GridBagLayout 布局
		this.setLayout(new GridBagLayout());
		//将 tree 添加到无边框的滚动面板中
		SmNoBorderScrollPanel scrollPanel = new SmNoBorderScrollPanel(tree);
		//添加滚动面板
		this.add(scrollPanel,new GridBagConstraintsHelper(0,0,1,1).setWeight(1,1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP,GridBagConstraintsHelper.FRAME_CONTROL_GAP,GridBagConstraintsHelper.FRAME_CONTROL_GAP,GridBagConstraintsHelper.CONTROLS_GAP)
				.setFill(GridBagConstraints.BOTH));
	}

	private void initListener(){
		//添加鼠标事件
		tree.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				//鼠标右键释放时，存在选中的节点，显示弹出菜单
				if (e.getButton() != MouseEvent.BUTTON3){
					return;
				}
				int[] selectionRows = tree.getSelectionRows();
				if (selectionRows == null || selectionRows.length ==0){
					return;
				}
				//根节点不允许删除
				deleteMenuItem.setEnabled(!containWorkspaceNode(selectionRows));
				addMenuItem.setEnabled(selectionRows.length==1);
				editMenuItem.setEnabled(selectionRows.length==1);
				popupMenu.show(e.getComponent(),e.getX(),e.getY());
			}

			//判断选中行中是否有根节点
			private boolean containWorkspaceNode(int[] selectionRows){
				for (int selectionRow : selectionRows) {
					if (selectionRow == 0){
						return true;
					}
				}
				return false;
			}
		});

		//创建菜单项点击事件
		ActionListener actionListener = e -> {
			if (e.getSource() == expandMenuItem){
				expand();
			}else if (e.getSource() == editMenuItem){
				edit();
			}else if (e.getSource() == addMenuItem){
				add();
			}else if (e.getSource() == deleteMenuItem){
				delete();
			}else if (e.getSource() == collapseMenuItem){
				collapse();
			}
		};

		//添加点击事件
		expandMenuItem.addActionListener(actionListener);
		editMenuItem.addActionListener(actionListener);
		addMenuItem.addActionListener(actionListener);
		deleteMenuItem.addActionListener(actionListener);
		collapseMenuItem.addActionListener(actionListener);
	}

	//展开树节点
	private void expand(){
		//获取选中的节点所在的行
		int[] rows = tree.getSelectionRows();
		if (rows ==null || rows.length==0){
			return;
		}
		for (int i = rows.length - 1; i >= 0; i--) {
			//展开节点
			tree.expandRow(rows[i]);
		}
	}

	//折叠选中数据
	private void collapse(){
		//获取选中的节点所在的行
		int[] rows = tree.getSelectionRows();
		if (rows != null && rows.length >0){
			for (int i = rows.length - 1; i >= 0; i--) {
				//折叠节点
				tree.collapseRow(rows[i]);
			}
		}
	}

	//编辑树节点
	private void edit(){
		int[] rows = tree.getSelectionRows();
		if (rows != null && rows.length != 1){
			//选中多个节点时不做编辑操作
			return;
		}
		//设置为可编辑状态
		tree.setEditable(true);
		//获取选中节点路径
		TreePath selectionPath = tree.getSelectionPath();
		//开始编辑节点
		tree.startEditingAtPath(selectionPath);
	}

	//在选中的节点添加子节点
	private void add(){
		int[] rows = tree.getSelectionRows();
		if (rows != null && rows.length != 1){
			//选中多个节点时不做添加操作
			return;
		}
		if (tree.getSelectionPath().getLastPathComponent() instanceof DefaultMutableTreeNode) {
			//获取选中节点
			DefaultMutableTreeNode mutableTreeNode = (DefaultMutableTreeNode) tree.getSelectionPath().getLastPathComponent();
			//创建新节点
			DefaultMutableTreeNode node = new DefaultMutableTreeNode("新节点" + (mutableTreeNode.getChildCount() + 1));
			//插入新节点
			((DefaultTreeModel)tree.getModel()).insertNodeInto(node,mutableTreeNode,mutableTreeNode.getChildCount());
		}

	}

	//删除选中节点
	private void delete(){
		//获取选中节点的路径
		TreePath[] selectionPaths = tree.getSelectionPaths();
		if (selectionPaths == null || selectionPaths.length ==0){
			return;
		}
		DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
		for (TreePath selectionPath : selectionPaths) {
			DefaultMutableTreeNode node = (DefaultMutableTreeNode) selectionPath.getLastPathComponent();
			if (node.getParent() != null){
				//移除选中节点
				model.removeNodeFromParent(node);
			}
		}
	}
}
